/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author Antonio
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "foto.cercaPerId", query = "SELECT f FROM Foto f WHERE f.id = ?1"),
    @NamedQuery(name = "foto.cercaPerNome", query = "SELECT f FROM Foto f Where f.nome=?1"),
    @NamedQuery(name = "foto.cercaTutto", query = "SELECT f FROM Foto f"),
    @NamedQuery(name = "foto.cercaFotoNonAssegnate", query = "SELECT f FROM Foto f WHERE NOT EXISTS "
    + "(SELECT m.id FROM Marchio m WHERE m.foto=f) AND NOT EXISTS (SELECT p.codiceBarre FROM Prodotto p WHERE p.foto=f)"),
    @NamedQuery(name = "foto.fotoProdotti", query = "SELECT p.foto FROM Prodotto p"),
    @NamedQuery(name = "foto.esisteFoto", query = "SELECT f FROM Foto f WHERE f.nome=?1 AND f.foto=?2"),
    @NamedQuery(name = "foto.fotoNoMarchi", query = "SELECT f FROM Foto f WHERE NOT EXISTS (SELECT m.id FROM Marchio m WHERE m.foto=f)")
})
public class Foto implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nome;
    @Lob
    private byte[] foto;

    public byte[] getFoto() {
        return foto;
    }

    public void setFoto(byte[] foto) {
        this.foto = foto;
    }

    /**
     * Get the value of nome
     *
     * @return the value of nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * Set the value of nome
     *
     * @param nome new value of nome
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Foto)) {
            return false;
        }
        Foto other = (Foto) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return id + "," + nome;
    }

    public String fotoPath() {
        return "http://localhost:8080/E-Market-war/ImageServlet?idFoto=" + id;
    }
}
